package com.ai.web.weixin.processor;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ai.web.cache.Constants;
import com.ai.web.service.WxUserService;
import com.ai.web.weixin.message.request.ClickMessage;
import com.ai.web.weixin.message.request.ReqBaseMessage;
import com.ai.web.weixin.message.response.Article;
import com.ai.web.weixin.message.response.NewsMessage;
import com.ai.web.weixin.message.response.TextMessage;
import com.ai.web.weixin.message.util.MessageUtil;
import com.tiny.persistence.domain.WxKeywordConfig;
import com.tiny.persistence.domain.WxRoleMenu;
import com.tiny.persistence.domain.WxUser;
import com.tiny.persistence.repository.WxKeywordConfigRepo;
import com.tiny.persistence.repository.WxRoleMenuRepo;

@Service(value="clickMsgProcessor")
public class ClickMsgProcessor implements IMessageProcessor {

	private static Logger logger = Logger.getLogger(ClickMsgProcessor.class);
	
	@Autowired
	private WxKeywordConfigRepo wxKeywordConfigRepo;
	@Autowired
	private WxRoleMenuRepo wxRoleMenuRepo;
	 @Autowired
	 private WxUserService wxUserService;
	 
	public String process(ReqBaseMessage bm) {
		
		logger.info("[Click Processor] " + bm.toString());

		ClickMessage cm = (ClickMessage) bm;
		String eventKey = cm.getEventKey();
		logger.info("[Click eventKey] " +eventKey);
		WxUser user =  wxUserService.getByOpenId(bm.getFromUserName());
		if(user == null){
			TextMessage nm = new TextMessage();
			nm.setToUserName(bm.getFromUserName());
			nm.setFromUserName(bm.getToUserName());
			nm.setCreateTime(System.currentTimeMillis());
			nm.setMsgType(MessageUtil.REQ_MESSAGE_TYPE_TEXT);
			nm.setContent("对不起,您没有操作该功能的权限!");
			return MessageUtil.textMessageToXml(nm);
		}
		List<WxRoleMenu> list = wxRoleMenuRepo.getByKeyword(user.getWxRole().getId(), eventKey);
		logger.info("[Click WxRoleMenu] " + list);
		if(list == null || list.size() == 0){
			TextMessage nm = new TextMessage();
			nm.setToUserName(bm.getFromUserName());
			nm.setFromUserName(bm.getToUserName());
			nm.setCreateTime(System.currentTimeMillis());
			nm.setMsgType(MessageUtil.REQ_MESSAGE_TYPE_TEXT);
			nm.setContent("对不起,您没有操作该功能的权限!");
			return MessageUtil.textMessageToXml(nm);
		}
		NewsMessage nm = new NewsMessage();
		nm.setToUserName(bm.getFromUserName());
		nm.setFromUserName(bm.getToUserName());
		nm.setCreateTime(System.currentTimeMillis());
		nm.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
		
		WxKeywordConfig config = wxKeywordConfigRepo.findByKeyword(eventKey);
		System.out.println("wxKeywordConfig=" + config);
		
		List<Article> articles = new ArrayList<Article>();
		Article article = new Article();
		article.setTitle(config.getTitle());
		article.setDescription(config.getDescription());
		article.setPicUrl(Constants.SERVER_HOST + config.getPicUrl());
		article.setUrl(Constants.SERVER_HOST + config.getLinkUrl()+"?openId=" + bm.getFromUserName());
		articles.add(article);
		nm.setArticleCount(articles.size());
		nm.setArticles(articles);
		return MessageUtil.newsMessageToXml(nm);
	}

}
